package com.cold.demo.generator.mybatis;

import com.cold.demo.generator.mybatis.utils.ModelToFileUtil;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyc
 */
public class CrudCreator {
    private String   packageRootName;
    private String   projectPathRoot;
    private String   controllerPath      = null;
    private String   servicePath         = null;
    private String   serviceImplPath     = null;
    private String   controllerPathTemp  = null;
    private String   servicePathTemp     = null;
    private String   serviceImplPathTemp = null;
    private String[] modeNames;
    private String[] restful;

    public CrudCreator(String[] tableNames, String packageRoot, String projectPathRoot) {
        this(Arrays.asList(tableNames), packageRoot, projectPathRoot);
    }

    public CrudCreator(List<String> tableNames, String packageRoot, String projectPathRoot) {
        super();
        this.packageRootName = packageRoot;
        this.projectPathRoot = projectPathRoot;
        //带下划线的
        modeNames = new String[tableNames.size()];
        for (int i = 0; i < tableNames.size(); i++) {
            //将表名全部先进行小写，再将下划线去掉最后将下划线后的第一个字母变成大写
            modeNames[i] = this.getModeNameByTableName(tableNames.get(i));
        }
        initPath();
    }


    public void creator() {
        for (int i = 0; i < modeNames.length; i++) {
            String              modeName = modeNames[i];
            Map<String, Object> map      = new HashMap<>(16);
            map.put("packageRootName", packageRootName);
            map.put("modelBig", modeName);
            map.put("modelSmall", firstCharToLowerCase(modeName));
            map.put("restful", restful[i]);
            map.put("controllerClassName", createControllerName(modeName));
            String modeServiceFilePath     = servicePath + "/I" + modeName + "Service.java";
            String modeServiceImplFilePath = serviceImplPath + "/" + modeName + "ServiceImpl.java";
            String modeControllerFilePath  = controllerPath + "/" + createControllerName(modeName) + "Controller.java";
            ModelToFileUtil.createFile(servicePathTemp, modeServiceFilePath, map);
            ModelToFileUtil.createFile(serviceImplPathTemp, modeServiceImplFilePath, map);
            ModelToFileUtil.createFile(controllerPathTemp, modeControllerFilePath, map);
        }
    }


    /**
     * 通过表名获取对象名字
     * 将表名全部先进行小写，再将下划线去掉最后将下划线后的第一个字母变成大写
     *
     * @param tableName
     * @return
     */
    private String getModeNameByTableName(String tableName) {
        tableName = tableName.toLowerCase();
        StringBuilder builder = new StringBuilder();
        char[]        chars   = tableName.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (i == 0) {
                aChar = (char) (aChar - 32);
            } else {
                if (aChar == '_' && i < chars.length - 1) {
                    aChar = chars[++i];
                    if (aChar < 'z' && aChar > 'a') {
                        aChar = (char) (aChar - 32);
                    }
                }
            }
            builder.append(aChar);
        }
        return builder.toString();
    }

    private String firstCharToLowerCase(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    private String createControllerName(String str) {
        str = str.substring(2);
        return str;
    }

    private void initPath() {
        //初始化生成代码的 controller service(包含了接口以及实现类) 的包路径

        String packagePath = packageRootName.replace(".", "/");
        String srcRootFile = new File(projectPathRoot, "/src").getPath();
        controllerPath = srcRootFile + "/" + packagePath + "/controller";
        servicePath = srcRootFile + "/" + packagePath + "/service";
        serviceImplPath = srcRootFile + "/" + packagePath + "/service/impl";
        //生成代码的模板的路径
        String templatesPath = CrudCreator.class.getClassLoader().getResource("templates").getPath().substring(1);
        controllerPathTemp = templatesPath + "/Controller.ftl";
        servicePathTemp = templatesPath + "/Service.ftl";
        serviceImplPathTemp = templatesPath + "/ServiceImpl.ftl";


        restful = new String[modeNames.length];
        for (int i = 0; i < modeNames.length; i++) {
            String       modeName = modeNames[i];
            StringBuffer buffer   = new StringBuffer();
            char[]       chars    = modeName.toCharArray();
            for (char aChar : chars) {
                if (aChar >= 'A' && aChar <= 'Z') {
                    //大写
                    buffer.append("/").append((char) (aChar + 32));
                } else {
                    buffer.append(aChar);
                }
            }
            restful[i] = buffer.toString().substring(3);
        }
    }


}
